package com.yizhubao.app.activitys;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yizhubao.app.MainActivity;
import com.yizhubao.app.R;
import com.yizhubao.app.app.YZBApp;
import com.yizhubao.app.base.BaseActivityToolBar;
import com.yizhubao.app.beans.ConfigBean;
import com.yizhubao.app.customviews.AgreementDialog;
import com.yizhubao.app.customviews.inter.CompleteCallBack;
import com.yizhubao.app.customviews.inter.OthersCallBack;
import com.yizhubao.app.models.ConfigModel;
import com.yizhubao.app.utils.ConfigObserver;
import com.yizhubao.app.utils.Constants;
import com.yizhubao.app.utils.Utils;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.JzvdStd;
import h.framework.pacy.utils.LogUtil;
import h.framework.pacy.utils.PreferencesUtil;
import h.framework.pacy.utils.ScreenUtil;

/**
 * 创建人: PACY
 * 创建时间: 6/6/21 10:25 AM
 * 邮箱: pengwwe457621023@qq.com
 * 类描述: 新闻介绍页
 */
public class NewsActivity extends BaseActivityToolBar {
    @BindView(R.id.jz_video)
    JzvdStd jzvdStd;
    @BindView(R.id.title)
    TextView title;

    @BindView(R.id.video_cv)
    CardView video_cv;
    ConfigModel model;

    @Override
    protected int setContentLayout() {
        return R.layout.activity_news;
    }

    @Override
    protected void initView() {
        setTitleOnLeftIcon(R.drawable.icon_back,"");
        ViewGroup.LayoutParams params=video_cv.getLayoutParams();
        params.width= ScreenUtil.getScreenWidth(NewsActivity.this)-ScreenUtil.dp2px(44);
        double temp=(double)params.width/16;
        params.height=(int)(temp*9)-1;
        video_cv.setLayoutParams(params);
        LogUtil.i("params.width="+params.width+",params.height="+params.height);
//        FrameLayout.LayoutParams params2= (FrameLayout.LayoutParams) jzvdStd.getLayoutParams();
//        params2.width= ScreenUtil.getScreenWidth(NewsActivity.this)-ScreenUtil.dp2px(40);
//        params2.height=(params2.width/16)*9;
//        jzvdStd.setLayoutParams(params2);
    }

    @Override
    protected void initData() {
        model=new ConfigModel();
        sendRequest();
    }

    void sendRequest(){
        model.getConfig(new ConfigObserver() {
            @Override
            public void onSuccess(ConfigBean t) {
                if(t.isShowNewsVideo()){
                    video_cv.setVisibility(View.VISIBLE);
                    jzvdStd.setUp(Constants.NEWS_VIDEO_URL
                            , "");
                    jzvdStd.startVideo();
                    title.setText(t.getNewsTitle());
                }else{
                    video_cv.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(String errMessage) {

            }
        },getLifeSubject());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        jzvdStd.releaseAllVideos();
        if (jzvdStd.backPress()) {
            return;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        jzvdStd.releaseAllVideos();
    }

    @OnClick(value = {R.id.submit_tv})
    public void click(View v){
        if(v.getId()==R.id.submit_tv){
            if(Utils.isFirst()){
                //首次进入
                AgreementDialog.DialogBuilder(NewsActivity.this).completeCallBack(() -> {
                    PreferencesUtil.put(Constants.FIRST,false);
                    startActivity(NewsActivity.this, MainActivity.class);
                    YZBApp.getInstance().removeWelcome();
                    finish();
                }).othersCallBack(() -> {
                }).show();
            }else{
                startActivity(NewsActivity.this, MainActivity.class);
                finish();
            }
        }
    }
}